//
//  XDPracticeViewController.m
//  XDEducation
//
//  Created by apple on 2017/4/17.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "XDPracticeViewController.h"
#import "XDPracticeChineseModel.h"
#import "XDPracticeEnglishModel.h"
#import <AVFoundation/AVFoundation.h>
@interface XDPracticeViewController ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UILabel *wordsLabel;       //单词
@property (weak, nonatomic) IBOutlet UIButton *voiceBtn;        //语音按钮
@property (weak, nonatomic) IBOutlet UILabel *wordGroupLabel;   //词组
@property (weak, nonatomic) IBOutlet UITextField *inputTextField;   //输入框
@property (weak, nonatomic) IBOutlet UIButton *resetBtn;            //重写按钮
@property (weak, nonatomic) IBOutlet UIButton *nextBtn;         //下一个按钮


/** 当前数据源 */
@property(nonatomic,strong) NSMutableArray *currentDataArr;

@end

@implementation XDPracticeViewController
{
    NSInteger _currentPage;     //当前数据
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    _currentPage = 0;
    
    self.inputTextField.delegate = self;
    [self initNetWork];
}


-(NSMutableArray *)currentDataArr{
    if(!_currentDataArr){
        _currentDataArr = [NSMutableArray array];
    }
    return _currentDataArr;
}


/**
 刷新界面
 */
-(void)reloadView:(NSMutableArray *)dataArr{
    
    if (self.practiceType == PracticeTypeChinese) {
        
        XDPracticeChineseModel *model = dataArr[_currentPage];
        self.wordsLabel.text = model.fontname;
        
        NSString *wordGroup = model.fontdesc;
        if ([wordGroup isKindOfClass:[NSNull class]]) {
            wordGroup = @"";
        }
        self.wordGroupLabel.text = [NSString stringWithFormat:@"[词组]:%@",wordGroup];
        
    }else{
        
        XDPracticeEnglishModel *model = dataArr[_currentPage];
        self.wordsLabel.text = model.word;
        
        NSString *wordGroup = model.meaning;
        if ([wordGroup isKindOfClass:[NSNull class]]) {
            wordGroup = @"";
        }
        self.wordGroupLabel.text = [NSString stringWithFormat:@"[语]:%@",wordGroup];
    
    }
    

   
    
}

//读
- (IBAction)voiceBtnClicked:(id)sender {
    
    
    NSString *readstr;
    if(self.practiceType == PracticeTypeChinese){
        XDPracticeChineseModel *model = self.currentDataArr[_currentPage];
        readstr = model.fontname;
    }else{
        XDPracticeEnglishModel *model = self.currentDataArr[_currentPage];
        readstr = model.word;
    }
    
    
    
    AVSpeechSynthesizer *speech = [[AVSpeechSynthesizer alloc]init];
    AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc]initWithString:readstr];  //需要转换的文本
    [speech speakUtterance:utterance];
    
}

//重写
- (IBAction)resetBtnClicked:(id)sender {
    
    self.inputTextField.text = @"";
}

//下一个
- (IBAction)nextBtnClicked:(id)sender {
    _currentPage += 1;
   
    if (_currentPage >= self.currentDataArr.count) {
        
        [self.navigationController popViewControllerAnimated:YES];
        
    }else{
        
        if(_currentPage == self.currentDataArr.count - 1){
            //最后一个单词
            [self.nextBtn setTitle:@"完成" forState:UIControlStateNormal];
        }
        [self reloadView:self.currentDataArr];
    }
}



/**
 网络请求
 */
-(void)initNetWork{

    NSString *url;
    if(self.practiceType == PracticeTypeEnglish){   //单词
        url = BaseURL(@"exercise.do?action=getwords");
    }else{      //汉字
        url = BaseURL(@"exercise.do?action=getfonts");
    }
    //都是从上一个页面带过来的值
    NSDictionary *dict = @{@"unitid":self.unitId,
                          @"gradeid":self.gradeId,
                          @"termtype":self.termId,
                          @"lessonid":self.lessonId,     
                          @"uid":[AccountTool shareAccountTool].currentAccount.uid};
    [MBProgressHUD showHUD];
    [XDHttpTool get:url authCode:[AccountTool shareAccountTool].currentAccount.authCode params:dict success:^(id responseObj) {
        [MBProgressHUD hideHUD];
        //NSLog(@"%@",responseObj);
        NSArray *dictArr = responseObj;
        
        NSMutableArray *practiceModelArr;
        if (self.practiceType == PracticeTypeChinese) {     //汉字练习
            practiceModelArr = [XDPracticeChineseModel mj_objectArrayWithKeyValuesArray:dictArr];
        }else{                  //单词联系
             practiceModelArr = [XDPracticeEnglishModel mj_objectArrayWithKeyValuesArray:dictArr];
        
        }
        
        
        self.currentDataArr = practiceModelArr;
        if (practiceModelArr.count ==0) {
            
            [MBProgressHUD showError:XDNetWorkError];
        }else{
            //刷新界面
            [self reloadView:practiceModelArr];
        }
        
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUD];
        [MBProgressHUD showError:XDNetWorkError];
        NSLog(@"%@",error);
    }];
}



#pragma - mark  TextField 代理方法

//弹回键盘
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    //设置动画的名字
    [UIView beginAnimations:@"Animation" context:nil];
    //设置动画的间隔时间
    [UIView setAnimationDuration:0.20];
    //??使用当前正在运行的状态开始下一段动画
    [UIView setAnimationBeginsFromCurrentState: YES];
    //设置视图移动的位移
    self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y - 200, self.view.frame.size.width, self.view.frame.size.height);
    //设置动画结束
    [UIView commitAnimations];
}

//在UITextField 编辑完成调用方法
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    
    self.inputTextField.text = textField.text;
    //设置动画的名字
    [UIView beginAnimations:@"Animation" context:nil];
    //设置动画的间隔时间
    [UIView setAnimationDuration:0.20];
    //??使用当前正在运行的状态开始下一段动画
    [UIView setAnimationBeginsFromCurrentState: YES];
    //设置视图移动的位移
    self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y + 200, self.view.frame.size.width, self.view.frame.size.height);
    //设置动画结束
    [UIView commitAnimations];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
   
    [self.view endEditing:YES];
}

@end
